package com.hdoit.ares.web.official.providers;

import java.util.ArrayList;
import java.util.List;

import com.hdoit.ares.logical.official.model.ArticalClassify;
import com.hdoit.ares.logical.official.service.IArticalClassifyService;
import com.tennetcn.common.aop.CommonApplicationContextUtil;
import com.tennetcn.common.message.TreeListItem;
import com.tennetcn.component.attribute.provider.ISingleOptionListProvider;

public class ArticalClassifyTreeProvider implements ISingleOptionListProvider<TreeListItem>{
	private List<TreeListItem> treeListItems=null;
	@Override
	public List<TreeListItem> getDataList(Object viewData) {
		List<ArticalClassify> menus=getMenus();
		
		treeListItems =new ArrayList<TreeListItem>();
		TreeListItem treeListItem=TreeListItem.getTopLevel();
		treeListItems.add(treeListItem);
		
	    if (menus == null) { return treeListItems; }

	    for (ArticalClassify menu: menus) {
	    	treeListItem=new TreeListItem();
	    	treeListItem.setId(menu.getId());
	 		treeListItem.setParentId(menu.getParentId());
	 		treeListItem.setText(menu.getName());
	 		treeListItem.setValue(menu.getId());
	 		treeListItem.setClick(true);
	 		treeListItem.setOpen(true);
	 		treeListItems.add(treeListItem);
		}
	    return treeListItems;
	}

	@Override
	public TreeListItem getSelectedItem(Object value, Object viewData) {
		if(value==null){return null;}
		
		for (TreeListItem treeListItem : treeListItems) {
			if(treeListItem.getValue().equals(value)){
				return treeListItem;
			}
		}
		return null;
	}
	
	private List<ArticalClassify> getMenus(){
		IArticalClassifyService menuService=CommonApplicationContextUtil.getCurrentContext().getBean(IArticalClassifyService.class);
		return menuService.queryListTopMenu();
	}
}
